/**
 *  Copyright (c)  2011-2020 Panguso, Inc.
 *  All rights reserved.
 *
 *  This software is the confidential and proprietary information of Panguso, 
 *  Inc. ("Confidential Information"). You shall not
 *  disclose such Confidential Information and shall use it only in
 *  accordance with the terms of the license agreement you entered into with Panguso.
 */

/*

 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.edu.thu.log.filter.logfilter;


import cn.edu.thu.log.read.LogBufferModified;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import storage.dbhelper.ActivityAbstractHelper;
import storage.dbhelper.LogBodyHelper;
import storage.dbhelper.LogHeadHelper;
import storage.dbobject.DbActivityAbstractRule;
import storage.dbobject.DbLogBody;

/**
 * 
 * @author wan
 */
public class ReadContentByTagName {

	private Map<String, String> bodytagMap = null;
	private ArrayList<String> classifyTagList = null;
	private Map<String, DbActivityAbstractRule> activitypool = null;
	private Map<String, DbLogBody> logbodypool = null;
/**
 * 
 */
	public ReadContentByTagName() {
		classifyTagList = new ArrayList<String>();
		bodytagMap = new HashMap<String, String>();
		logbodypool = new HashMap<String, DbLogBody>();
		activitypool = new HashMap<String, DbActivityAbstractRule>();
	}
/**
 * 
 * @param tagname parameter
 * @param record parameter
 * @return
 */
	public String getAndCutContentByTagName(String tagname, LogBufferModified record) {
	
			String abbreviation = "";
			// bodytagMap=record.getLogBodyMap();//对应key是tagname，value是缩写
			if (bodytagMap.get(tagname) != null) {
				abbreviation = bodytagMap.get(tagname);
				// System.out.println("abbreviation:"+abbreviation);
			} else {
				if (!logbodypool.containsKey(tagname)) {
					LogBodyHelper logBodyH = new LogBodyHelper();
					List<DbLogBody> logBodys = logBodyH.getLogBodys(
							DbLogBody.CL_NAME, tagname);
					// DbLogBody logBody;
					if (logBodys.size() > 0) {
						DbLogBody xxl = logBodys.get(0);
						logbodypool.put(tagname, xxl);
					}
				}
				DbLogBody logBody = logbodypool.get(tagname);
				abbreviation = logBody.getAbbreviation();
				bodytagMap.put(tagname, abbreviation);

			}
			String bodycontent = record.getContent(abbreviation);
			// System.out.println("tagname:"+tagname+" 获取的内容是：" + bodycontent);
			if (bodycontent == null || bodycontent.equals("")
					|| bodycontent.equals("null")) {
				return bodycontent;
			} else if ((tagname.equals("URL") || tagname.equals("SearchUrl"))
					&& classifyTagList.contains(tagname)) {
				String partBodyContent = URLAbstraction.getPartURL(bodycontent);
				// 修改record
				record.addkeyvalue(abbreviation, partBodyContent);	
				return partBodyContent;
			} else if (classifyTagList.contains(tagname)) { // 对有分段规则的字段进行分段		
				DbActivityAbstractRule activityAbstractRule = activitypool
						.get(tagname);
				ArrayList<String> classifyRules = activityAbstractRule
						.getAbstractRuleList();
				String partcontent = "";
				for (String classifyRule : classifyRules) {
					// System.out.println("classifyRule:" + classifyRule +
					// " bodycontent:" + bodycontent);
					String[] temp = classifyRule.split("to");
					// 如果在这个区间，则返回这个区间规则
					if (temp[0].equals("@@") && (temp[1].equals("@@"))) {
						return bodycontent;
					}
					if (between(temp, bodycontent)) {
						// System.out.println("partcontent结果："+classifyRule);
						if (temp[0].equals("@@")) {
							partcontent = "小于" + temp[1];
						}
						if (temp[1].equals("@@")) {
							partcontent = "大于" + temp[0];
						} else {
							partcontent = classifyRule;
						}
						break;
					}
				}
				if (partcontent.equals("")) {
					partcontent = "else";
				}
				record.addkeyvalue(abbreviation, partcontent);
				// System.out.println(record.getLogBodyMap().get(abbreviation));
				return partcontent;
			} else {
				return bodycontent;
			}
		
	}

	private boolean between(String[] rule, String content) {
		int bvalue0 = 0;
		int bvalue1 = 0;
		int tagvalueInteger = 0;
		try {
			if (rule[0].equals("@@")) {
				bvalue0 = Integer.MIN_VALUE;
			} else {
				bvalue0 = Integer.parseInt(rule[0]);
			}
			if (rule[1].equals("@@")) {
				bvalue1 = Integer.MAX_VALUE;
			} else {
				bvalue1 = Integer.parseInt(rule[1]);
			}
			tagvalueInteger = Integer.parseInt(content);
		} catch (Exception e) {
			return false;
		}
		if ((bvalue0 < bvalue1) && (bvalue0 < tagvalueInteger)
				&& (tagvalueInteger < bvalue1)) {
			// System.out.println("bvalue0<content<bvalue1");
			return true;
		} else {
			return ((bvalue0 > bvalue1) && (bvalue1 < tagvalueInteger) && (tagvalueInteger < bvalue0));
		}
		
		/*else if ((bvalue0 > bvalue1) && (bvalue1 < tagvalueInteger)
				&& (tagvalueInteger < bvalue0)) {
			// System.out.println("bvalue0>content>bvalue1");
			return true;
		} else {
			return false;
		}*/
	}
	

	/**
	 * @return the classifyTagList
	 */
	public ArrayList<String> getClassifyTagList() {
		return classifyTagList;
	}

	/**
	 * @param classifyTagList
	 *            the classifyTagList to set
	 */
	public void setClassifyTagList(ArrayList<String> classifyTagList) {
		this.classifyTagList = classifyTagList;
		ActivityAbstractHelper activityAbstractHelper = new ActivityAbstractHelper();
		LogBodyHelper logBodyH = new LogBodyHelper();
		System.out.println("Classifly TagList:" + classifyTagList.toString());
		for (String tagname : this.classifyTagList) {
			List<DbActivityAbstractRule> activityAbstractRulesss = activityAbstractHelper
					.getActivityAbstractRules(DbActivityAbstractRule.CL_NAME,
							tagname);
			if (activityAbstractRulesss != null
					&& activityAbstractRulesss.size() > 0) {
				activitypool.put(tagname, activityAbstractRulesss.get(0));
			}
		}
	
	}

}
